<template>
  <el-form ref="form" :model="userDetails" :rules="rules" label-width="100px">
    <el-form-item label="学号/工号" prop="number">
      <el-input v-model="userDetails.number" maxlength="30" disabled/>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="userDetails.name" maxlength="11" disabled/>
    </el-form-item>
    <el-form-item label="所在系" prop="email">
      <el-input v-model="userDetails.faculty" maxlength="50" />
    </el-form-item>
    <el-form-item label="身份标识">
      <el-select v-model="userDetails.identity" placeholder="请选择身份标识" disabled>
        <el-option
          v-for="dict in identityOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import { updateUserDetails } from "@/api/system/user";

  export default {
    props: {
      userDetails: {
        type: Object
      }
    },
    data() {
      return {
        // 身份表示 1学生 2老师 3工作人员 4管理员字典
        identityOptions: [],
        // 表单校验
        rules: {
          number: [
            { required: true, message: "学号/工号不能为空", trigger: "blur" }
          ],
          name: [
            { required: true, message: "姓名不能为空", trigger: "blur" }
          ],
          identity: [
            { required: true, message: "请选择身份标识", trigger: "blur" }
          ]
        }
      };
    },
    created(){
      this.getDicts("user_identity").then(response => {
        this.identityOptions = response.data;
      });
    },
    methods: {
      submit() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateUserDetails(this.userDetails).then(response => {
              this.msgSuccess("修改成功");
            });
          }
        });
      },
      close() {
        this.$store.dispatch("tagsView/delView", this.$route);
        this.$router.push({ path: "/index" });
      }
    }
  };
</script>
